package com.fivetalks.client;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.fivetalks.object.Member;
import com.fivetalks.object.RoomInfo;

public class ShowReadyRoomActivity extends Activity implements OnClickListener, Runnable{

	private ArrayList<RoomInfo> roomList;
	Thread m_thread;
	public static Member member;
	private CustomAdapter mAdapter;
	private ListView list;
	String title = null;

	Button btMakeRoom;
	Button btGoReadyRoom;
	Button btShowAllRoom;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_ready_room);

//		Intent intent = getIntent();
//		member = (Member) intent.getSerializableExtra("member");

		list = (ListView) findViewById(R.id.list);
		roomList = new ArrayList<RoomInfo>();
		mAdapter = new CustomAdapter(this, R.layout.list_view_row, roomList,
				member);
		list.setAdapter(mAdapter);

		btMakeRoom = (Button) findViewById(R.id.btMakeRoom);
		btGoReadyRoom = (Button) findViewById(R.id.btGoReadyRoom);
		btShowAllRoom = (Button) findViewById(R.id.btShowAllRoom);
		btMakeRoom.setOnClickListener(this);
		btGoReadyRoom.setOnClickListener(this);
		btShowAllRoom.setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		start();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.btMakeRoom:
			intent = new Intent(this, RoomDialogActivity.class);
			startActivity(intent);
			break;

		case R.id.btGoReadyRoom:

			break;

		case R.id.btShowReadyRoom:
			intent = new Intent(this, ChatLobbyActivity.class);
			startActivity(intent);
			break;
		}
	}

	public void start() {
		m_thread = new Thread(this);
		m_thread.start();
	}

	public void addResult(RoomInfo room) {
		roomList.add(room);
		refreshView();
	}

	public void refreshView() {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mAdapter.notifyDataSetChanged();
			}
		});
	}

	@Override
	protected void onPause() {
		super.onPause();

	}

	public void run() {
		SocketClient sc = SocketClient.getInstance(null,null,null,null,null,null,null,null);

		try {
			roomList.clear();
			refreshView();

			Log.d("lobby", "before_while");
			while (true) {
				Log.d("lobby", "while_start");
				title = sc.getDis().readUTF();
				Log.d("lobby", "title: " + title);
				if ("--endRoomList--".equals(title)) {
					break;
				}
				RoomInfo room = new RoomInfo();
				room.setTitle(title);

				room.setRoomNumber(Long.parseLong(sc.getDis().readUTF()));
				room.setLimitCount(Integer.parseInt(sc.getDis().readUTF()));
				room.setCurrMale(Integer.parseInt(sc.getDis().readUTF()));
				room.setCurrFemale(Integer.parseInt(sc.getDis().readUTF()));
				room.setStatus(sc.getDis().readUTF());
				
				if("Ready".equals(room.getStatus()))
					addResult(room);
			}
			sc.getDos().flush();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
